#include "texedit.h"

#include <QtWidgets>
#include <Qsci/qscilexertex.h>

TexEdit::TexEdit(QString content, QWidget *parent)
    : QDialog(parent) {
    setWindowIcon(QIcon(":/manim-studio.ico"));
    texEdit= new QsciScintilla;
    setHighliter();
    auto okButton = new QPushButton(tr("OK"));
    auto cancelButton = new QPushButton(tr("Cancel"));

    auto gLayout = new QGridLayout;
    gLayout->setColumnStretch(1, 2);

    gLayout->addWidget(texEdit, 1, 1, Qt::AlignLeft);

    auto buttonLayout = new QHBoxLayout;
    buttonLayout->addWidget(okButton);
    buttonLayout->addWidget(cancelButton);
    gLayout->addLayout(buttonLayout, 2, 1, Qt::AlignRight);

    auto mainLayout = new QVBoxLayout;
    mainLayout->addLayout(gLayout);
    setLayout(mainLayout);

    connect(okButton, &QAbstractButton::clicked, this, &QDialog::accept);
    connect(cancelButton, &QAbstractButton::clicked, this, &QDialog::reject);

    setWindowTitle(tr("Edit the Tex content"));
    texEdit->append(content);
}

void TexEdit::setHighliter(){
    QsciLexerTeX *texLexer = new QsciLexerTeX;
    texEdit->setLexer(texLexer);

    texEdit->setMarginType(0,QsciScintilla::NumberMargin);
    texEdit->setMarginLineNumbers(0,true);
    texEdit->setMarginWidth(0,15);

    texEdit->setAutoCompletionSource(QsciScintilla::AcsAll);
    texEdit->setAutoCompletionCaseSensitivity(true);
    texEdit->setAutoCompletionThreshold(2);

    QFont fonts;
    fonts.setFixedPitch(true);
    texEdit->setFont(fonts);
}
